package com.malou.client.reportes;

import java.util.HashMap;

import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Frame;
import com.malou.client.MalouConstants;

public class XlsReportDownload extends FormPanel {
	private Frame frame;
	private MalouConstants constants = GWT.create(MalouConstants.class);
	
	public XlsReportDownload(String fileName, HashMap<String, Object> param, final Status status, final Button excel, final Integer consulta_id){
		setLayout(new FitLayout());
		//setHeading(title);
		setFrame(true);
		setHeaderVisible(false);
		frame = new Frame();
		frame.setVisible(false);
		add(frame);
		
		AsyncCallback<String> reportURLCallback = new AsyncCallback<String>(){
			
			MessageBox messageBox = new MessageBox();

			@Override
			public void onFailure(Throwable caught) {
				messageBox.setMessage(constants.errorConexion());
				messageBox.show();
				caught.printStackTrace();
				if(status != null)	status.hide();				
			}

			@Override
			public void onSuccess(String result) {
				if (result != null){					
					frame.setUrl(result);					
				}else{
					messageBox.setMessage(constants.noSePudoCargarReporte());
					messageBox.show();
				}
				if(status != null)	status.hide();				
				if(excel != null)	excel.show();
			}
			
		};
		
		((ReporteServiceAsync) GWT.create(ReporteService.class)).getXlsReport(fileName, param, consulta_id, reportURLCallback);		
	}
}
